package cloud.daodao.storm.user.helper;

import cloud.daodao.storm.common.security.api.CaptchaApi;
import cloud.daodao.storm.common.security.model.captcha.CaptchaParam;
import cloud.daodao.storm.common.error.AppError;
import cloud.daodao.storm.common.error.AppException;
import cloud.daodao.storm.common.model.Ok;
import cloud.daodao.storm.common.model.Request;
import cloud.daodao.storm.common.model.Response;
import cloud.daodao.storm.common.user.error.UserError;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DictHelper {

    @Resource
    private CaptchaApi captchaApi;

    public void captchaCheck(CaptchaParam param) {
        Request<CaptchaParam> request = new Request<>(param);
        Response<Ok> response;
        try {
            response = captchaApi.captchaCheck(request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new AppException(UserError.INVOKE_DICT_EXCEPTION, e.getMessage());
        }
        if (!AppError.OK.code.equals(response.getCode())) {
            log.error("captchaApi.captchaCheck : {} : {}", request.toJson(), response.toJson());
            throw new AppException(UserError.LOGIN_CAPTCHA_ERROR, response.toJson());
        }
    }

}
